﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TerrariaSVCv2 {
    public class Worker {

        #region "Events and Delegates"
        public event ServerMessage onServerMessage;
        public delegate void ServerMessage(string message);

        //duck has joined
        public event PlayerJoin onPlayerJoin;
        public delegate void PlayerJoin(string username);

        //duck has left
        public event PlayerLeft onPlayerLeft;
        public delegate void PlayerLeft(string username);

        // <duck> sample chat from duck
        public event PlayerChat onPlayerChat;
        public delegate void PlayerChat(string username, string message);
        #endregion "Events and Delegates"

        public void StartService(object state) {
            //throw new NotImplementedException("This hasn't been implemented yet.");
            ExeWrapper exe = new ExeWrapper();
            exe.StartProcess();
        }

        public void StopService(object state) {
            throw new NotImplementedException("This hasn't been implemented yet.");
        }

        public void SendChat(string message) {
            try {
                throw new NotImplementedException("This hasn't been implemented yet.");
            } catch (Exception err) {
                Console.Error.WriteLine(err);
            }
        }
        
        public void SendCommand(string cmd) {
            try {
                throw new NotImplementedException("This hasn't been implemented yet.");
            } catch (Exception err) {
                Console.Error.WriteLine(err);
            }
        }

        public void SaveWorld() {
            throw new NotImplementedException("This hasn't been implemented yet.");
        }


        /*
        ProcessStartInfo buildProcess() {
            //base install directory, dynamic/args later
            //C:\Program Files (x86)\Steam\SteamApps\common\Terraria
            //C:\Program Files\Steam\SteamApps\common\Terraria

            psi = new ProcessStartInfo();
            psi.Arguments = string.Format("-config {0}", "serverconfig.txt"); //static for now.. dynamic later
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.FileName = "TerrariaServer.exe";

#warning "TerrariaServer.exe is hard coded for location but it might be okay..."
            if (File.Exists(@"C:\Program Files (x86)\Steam\SteamApps\common\Terraria\TerrariaServer.exe")) {
                psi.WorkingDirectory = @"C:\Program Files (x86)\Steam\SteamApps\common\Terraria\";
            } else if (File.Exists(@"C:\Program Files\Steam\SteamApps\common\Terraria\TerrariaServer.exe")) {
                psi.WorkingDirectory = @"C:\Program Files\Steam\SteamApps\common\Terraria\";
            } else {
                //not ready for this yet!
                this.RequestSvcStop = true;
            }

            return psi;
        }
        */

        void doWork() {
            
            throw new NotImplementedException("This hasn't been implemented yet.");
        }



    }
}
